	
(function($) {

		$.suggest = function(input, options) {
			var $input = $(input).attr("autocomplete", "off");
			var $results;

			var timeout = false;		
			var prevLength = 0;			
			var cache = [];			
			var cacheSize = 0;		
			
			if($.trim($input.val())=='' || $.trim($input.val())=='中文/拼音') $input.val('中文/拼音').css('color','#aaa');
			if( ! options.attachObject )
				options.attachObject = $(document.createElement("ul")).appendTo('body');

			$results = $(options.attachObject);
			$results.addClass(options.resultsClass);
			
//			resetPosition();
//			$(window)
//				.load(resetPosition)	
//				.resize(resetPosition);

			$input.blur(function() {
				setTimeout(function() { 
					$results.hide() 
					}, 200);
			});
			
			$input.focus(function(){
				if($.trim($(this).val())=='中文/拼音'){
					$(this).val('').css('color','#000');
				}
				if($.trim($(this).val())==''){
					displayItems('');
				}
			});
			$input.click(function(){
				var q=$.trim($(this).val());
				displayItems('');
				$(this).select();
			});
						
			try {
				$results.bgiframe();
			} catch(e) { }

			$input.keyup(processKey);//
			
//			$input.blur(selectCurrentResult);
//			function resetPosition() {
//				
//				var offset = $input.offset();
//				$results.css({
//					top: (offset.top + input.offsetHeight) + 'px',
//					left: offset.left + 'px'
//				});
//			}
			
			
			function processKey(e) {
				if ((/27$|38$|40$/.test(e.keyCode) && $results.is(':visible')) ||
					(/^13$|^9$/.test(e.keyCode) && getCurrentResult())) {
		            
		            if (e.preventDefault)
		                e.preventDefault();
					if (e.stopPropagation)
		                e.stopPropagation();

	                e.cancelBubble = true;
	                e.returnValue = false;
				
					switch(e.keyCode) {
	
						case 38:
							prevResult();
							break;
				
						case 40: 
							nextResult();
							break;
						case 13: 
							selectCurrentResult();
							break;
							
						case 27: 
							$results.hide();
							break;
	
					}
					
				} else if ($input.val().length != prevLength) {

					if (timeout) 
						clearTimeout(timeout);
					timeout = setTimeout(suggest, options.delay);
					prevLength = $input.val().length;
					
				}			
					
				
			}
			
			function suggest() {
			
				var q = $.trim($input.val());
				displayItems(q);
			}		
			function displayItems(items) {
				var html = '';
				if (items=='') {
					for(h in options.hot_list){
						html+='<li style="_width:20px;" rel="'+options.hot_list[h][0]+'"><a href="#'+h+'">'+options.hot_list[h][1]+'</a></li>';
					}
					html='<div class="gray ac_result_tip" style="font-size:14px;">热门城市（其他城市直接输入搜索）</div><ul>'+html+'</ul>';
				}
				else {
					for (var i = 0; i < options.source.length; i++) {
						var reg = new RegExp('^' + items + '.*$', 'im');
						if (reg.test(options.source[i][0]) || reg.test(options.source[i][1]) || reg.test(options.source[i][2]) || reg.test(options.source[i][3])) {
							html += '<li rel="' + options.source[i][0] + '" style="width:246px;">';
							html += '<a href="#'+ i +'"><em>' + options.source[i][1] + '</em>';
							html += '<span style="text-align:right;width:150px;">' + options.source[i][2] + '</span></a></li>';   
						}
					}
					if (html == '') {
						suggest_tip = '<div class="gray ac_result_tip">对不起，该地区暂不能投保，请重新输入！</div>';
						$("#comCode").val("");
					}
					else {
						suggest_tip = '<div class="gray ac_result_tip">' + items + '，按拼音排序</div>';
					}
					html = suggest_tip + '<ul>' + html + '</ul>';
					
					$input.blur(function(e) {
						$currentResult = getCurrentResult();
						
						if ($currentResult) {
						var resRes = $currentResult.children('a').html();		//热门城市选择
						var res = $currentResult.children('a').children('em').html();	//输入选择
						if(!resRes||res){
							e.preventDefault(); 
							e.stopPropagation();
							selectCurrentResult();
						}
					}
					});		
				}

				$results.html(html).show();
				$results.children('ul').children('li:first-child').addClass(options.selectClass);
				
				$results.children('ul')
					.children('li')
					.mouseover(function() {
						$results.children('ul').children('li').removeClass(options.selectClass);
						$(this).addClass(options.selectClass);
					})
					.click(function(e) {
						e.preventDefault(); 
						e.stopPropagation();
						selectCurrentResult();
					});
			}
						
			function getCurrentResult() {
				if (!$results.is(':visible'))
					return false;
			
				var $currentResult = $results.children('ul').children('li.' + options.selectClass);
				if (!$currentResult.length)
					$currentResult = false;
				return $currentResult;

			}
			
			function selectCurrentResult() {
				$currentResult = getCurrentResult();
			
				if ($currentResult) {
					var res = $currentResult.children('a').children('em').html();	//输入选择
					var resRes = $currentResult.children('a').html();	//热门城市选择
					if (res == null ) {
						$input.val(resRes);
					} else {
						$input.val(res);
					}
					
					$results.hide();

					if( $(options.dataContainer) ) {
						$(options.dataContainer).val($currentResult.attr('rel'));
						var comCOde = $currentResult.attr('rel');
						if (comCOde != '') {
							var license = new License ();
					        var licenseNo =  license.getLicensePlate(comCOde.substring(0,2));
					        //填充车牌号
					        if ($("#registered").attr("checked") == false) {
					        	$("#licensePlateNo").val(licenseNo); 
					        	$("#comCode").val(comCOde);
					        }
					        
					        var ifUrl = $("#ifUrl").attr('src');
					        if(ifUrl != null){
					        	quotePriceInsuranceSpecial();
					        }
						}
					}
	
					if (options.onSelect) {
						options.onSelect.apply($input[0]);
					}
				}
			}
			
			/** 特别优惠设定 */
			function sepicalInit(compStr){
				var spUrls = [
				  			'北京分公司#/tbth/7693.shtml',
				  			'深圳分公司#/tbth/7694.shtml',
				  			'宁波分公司#/tbth/7695.shtml',
				  			'江苏分公司#/tbth/7696.shtml',
				  			'上海分公司#/tbth/7697.shtml',
				  			'浙江分公司#/tbth/7698.shtml',
				  			'四川分公司#/tbth/7699.shtml',
				  			'湖北分公司#/tbth/7700.shtml',
				  			'天津分公司#/tbth/7701.shtml',
				  			'广东分公司#/tbth/7702.shtml',
				  			'陕西分公司#/tbth/7703.shtml',
				  			'河南分公司#/tbth/7704.shtml',
				  			'江西分公司#/tbth/7705.shtml',
				  			'重庆分公司#/tbth/7706.shtml',
				  			'云南分公司#/tbth/7707.shtml',
				  			'福建分公司#/tbth/7708.shtml',
				  			'青岛分公司#/tbth/7709.shtml',
				  			'辽宁分公司#/tbth/7710.shtml',
				  			'山东分公司#/tbth/7711.shtml',
				  			'黑龙江分公司#/tbth/7712.shtml',
				  			'大连分公司#/tbth/7713.shtml',
				  			'河北分公司#/tbth/7714.shtml',
				  			'内蒙古分公司#/tbth/7715.shtml',
				  			'甘肃分公司#/tbth/7716.shtml',
				  			'安徽分公司#/tbth/7717.shtml',
				  			'山西分公司#/tbth/7718.shtml',
				  			'吉林分公司#/tbth/7719.shtml',
				  			'广西分公司#/tbth/7720.shtml',
				  			'湖南分公司#/tbth/7721.shtml',
				  			'宁夏分公司#/tbth/7722.shtml',
				  			'贵州分公司#/tbth/7723.shtml',
				  			'海南分公司#/tbth/7724.shtml',
				  			'新疆分公司#/tbth/7725.shtml',
				  			'厦门分公司#/tbth/7726.shtml' ];
				
				for(var i=0; i<spUrls.length; i++){
					var spUrl = spUrls[i];
					var sps = spUrl.split("#");
					var currKey = sps[0];
					var currVal = sps[1];
					if(currKey.indexOf(compStr) != -1){
						$("#ifUrl").attr('src', currVal);
						break;
					}
				}
			}
			
			function nextResult() {
			
				$currentResult = getCurrentResult();
			
				if ($currentResult)
					$currentResult
						.removeClass(options.selectClass)
						.next()
							.addClass(options.selectClass);
				else
					$results.children('ul').children('li:first-child').addClass(options.selectClass);
			
			}
			
			function prevResult() {
			
				$currentResult = getCurrentResult();
			
				if ($currentResult)
					$currentResult
						.removeClass(options.selectClass)
						.prev()
							.addClass(options.selectClass);
				else
					$results.children('ul').children('li:last-child').addClass(options.selectClass);
			
			}
	
		}
		
		$.fn.suggest = function(source, options) {
			if (!source)
				return;
		
			options = options || {};
			options.source = source;
			options.hot_list=options.hot_list || [];
			options.delay = options.delay || 0;
			options.resultsClass = options.resultsClass || 'ac_results';
			options.selectClass = options.selectClass || 'ac_over';
			options.matchClass = options.matchClass || 'ac_match';
			options.minchars = options.minchars || 1;
			options.delimiter = options.delimiter || '\n';
			options.onSelect = options.onSelect || false;
			options.dataDelimiter = options.dataDelimiter || '\t';
			options.dataContainer = options.dataContainer || '#SuggestResult';
			options.attachObject = options.attachObject || null;
	
			this.each(function() {
				new $.suggest(this, options);
			});
	
			return this;
			
		};
		
	})(jQuery);
	
	